home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 53 / PC Actual CD 53.iso / VNULabs / ProgramacionSonido / WTest / DSUtiles.cpp next >
Encoding:
C/C++ Source or Header  |  2001-01-07  |  4.7 KB  |  179 lines

  1. // ============================================================
  2. //   LeaRNWaRe code by CROM / Spanish  Lords 
  3. //                            - since 1993 -
  4. //
  5. //   Objetivo   : Manejo de un buffer canalizado - Polling.
  6. //   Autor      : Pedro Ant≤n Alonso. crom@ergos.es
  7. //   Plataforma : Windows 95 / NT & Direct X
  8. //   Compilador : Visual C++ 6.0
  9. //
  10. // ============================================================
  11. #include "DSUtiles.h"
  12.  
  13. //
  14. // INIT
  15. //
  16. BOOL CDSUtil::Init( HWND hwnd, HINSTANCE hinst, GUID *pguid )
  17. {
  18.   HRESULT             hr; 
  19.   DSBUFFERDESC        DSBDesc;
  20.   LPDIRECTSOUNDBUFFER lpDSBPrimary;
  21.   WAVEFORMATEX        wavFormat;
  22.  
  23.  
  24.   bPlaying=FALSE;
  25.   CoInitialize (NULL);
  26.   if (FAILED(hr=DirectSoundCreate(pguid,&lpDS,NULL))) return FALSE;
  27.   if (FAILED(hr=lpDS->SetCooperativeLevel(hwnd,DSSCL_PRIORITY))) return FALSE;
  28.   ZeroMemory( &DSBDesc,sizeof(DSBUFFERDESC) );
  29.   DSBDesc.dwSize        = sizeof(DSBUFFERDESC);
  30.   DSBDesc.dwFlags       = DSBCAPS_PRIMARYBUFFER;
  31.   DSBDesc.dwBufferBytes = 0;
  32.   DSBDesc.lpwfxFormat   = NULL;
  33.   if( FAILED(hr=lpDS->CreateSoundBuffer(&DSBDesc,&lpDSBPrimary,NULL))) return FALSE;
  34.   ZeroMemory(&wavFormat,   sizeof(WAVEFORMATEX) ); 
  35.   wavFormat.wFormatTag      = WAVE_FORMAT_PCM; 
  36.   wavFormat.nChannels       = 2; 
  37.   wavFormat.nSamplesPerSec  = 22050; 
  38.   wavFormat.wBitsPerSample  = 16; 
  39.   wavFormat.nBlockAlign     = wavFormat.wBitsPerSample / 8 * wavFormat.nChannels;
  40.   wavFormat.nAvgBytesPerSec = wavFormat.nSamplesPerSec * wavFormat.nBlockAlign;
  41.   if(FAILED(hr=lpDSBPrimary->SetFormat(&wavFormat))) return FALSE;
  42.   lpDSBPrimary->Release();
  43.   return TRUE;
  44. }
  45.  
  46. //
  47. // END
  48. //
  49. void  CDSUtil::End ()
  50. {
  51.   StopPlay();
  52.   WaveCloseReadFile(&hmmio, &pWaveFormat);
  53.   CoUninitialize();
  54.   if (lpDSB) lpDSB->Release();
  55.   if (lpDS) lpDS->Release();
  56. }
  57.   
  58. //
  59. // PLAY
  60. //
  61. BOOL CDSUtil::Play ()
  62. {
  63.   HRESULT         hr;
  64.   DWORD           dwPlay;
  65.   DWORD           dwStartOffset;
  66.   VOID            *lpvData;
  67.   DWORD           dwBytesLocked;
  68.   UINT            uiBytesRead;
  69.  
  70.   if (lpDSB==NULL) return FALSE;
  71.  
  72.   if (FAILED(lpDSB->GetCurrentPosition(&dwPlay,NULL))) return FALSE;
  73.  
  74.   if (((dwPlay>=dwMiddleBuffer )&&(dwLastPlayPos<dwMiddleBuffer))||(dwPlay<dwLastPlayPos))
  75.   {
  76.     if (dwPlay>=dwMiddleBuffer) dwStartOffset = 0;
  77.     else                     dwStartOffset = dwMiddleBuffer;
  78.  
  79.     hr = lpDSB->Lock(dwStartOffset, dwMiddleBuffer,&lpvData,&dwBytesLocked,NULL,NULL,0);
  80.     WaveReadFile ( hmmio, dwBytesLocked,(BYTE *)lpvData,&mmckInfo,&uiBytesRead );
  81.     if (uiBytesRead<dwBytesLocked)
  82.     {
  83.       if (WaveStartDataRead(&hmmio,&mmckInfo,&mmckInfoParent)!=0) return FALSE;
  84.       else
  85.         WaveReadFile(hmmio,dwBytesLocked-uiBytesRead,(BYTE *)lpvData + uiBytesRead, 
  86.                     &mmckInfo,&uiBytesRead );    
  87.      }
  88.      lpDSB->Unlock(lpvData, dwBytesLocked, NULL, 0 );
  89.    }
  90.    dwLastPlayPos = dwPlay;
  91.    return TRUE;
  92. }
  93.  
  94. //
  95. // LOADWAVINBUFFER
  96. //
  97. BOOL CDSUtil::LoadWavInBuffer (LPSTR FileName)
  98. {
  99.   DSBUFFERDESC    DSBDesc;
  100.   HRESULT         hr;
  101.  
  102.   WaveCloseReadFile( &hmmio, &pWaveFormat );
  103.   if (lpDSB!=NULL)
  104.   {
  105.     lpDSB->Release();
  106.     lpDSB = NULL;
  107.   }
  108.  
  109.   if (WaveOpenFile(FileName,&hmmio,&pWaveFormat,&mmckInfoParent )!=0) return FALSE;
  110.   if (WaveStartDataRead(&hmmio,&mmckInfo,&mmckInfoParent)!=0) return FALSE;
  111.  
  112.   memset( &DSBDesc, 0, sizeof( DSBUFFERDESC ) ); 
  113.   DSBDesc.dwSize  = sizeof( DSBUFFERDESC ); 
  114.   DSBDesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS;
  115.   DSBDesc.dwBufferBytes = pWaveFormat->nAvgBytesPerSec*2;  
  116.   DSBDesc.lpwfxFormat   = pWaveFormat; 
  117.  
  118.   if (FAILED(hr=lpDS->CreateSoundBuffer(&DSBDesc,&lpDSB,NULL)))
  119.   {
  120.     WaveCloseReadFile(&hmmio,&pWaveFormat);
  121.     return FALSE; 
  122.   }
  123.  
  124.   ClearBuffer();
  125.   dwMiddleBuffer = DSBDesc.dwBufferBytes/2;
  126.   return TRUE;
  127. }
  128.  
  129. //
  130. // STARTPLAY
  131. //
  132. void CDSUtil::StartPlay()
  133. {
  134.   HRESULT         hr;
  135.  
  136.   if (bPlaying==FALSE)
  137.   {
  138.     hr = lpDSB->Play(0,0,DSBPLAY_LOOPING);
  139.     bPlaying=TRUE;
  140.   }
  141. }
  142.  
  143. //
  144. // STOPPLAY
  145. //
  146. void CDSUtil::StopPlay()
  147. {
  148.   HRESULT         hr;
  149.  
  150.   if (bPlaying==TRUE)
  151.   {
  152.     hr = lpDSB->Stop();
  153.     hr = lpDSB->SetCurrentPosition( 0L );    
  154.     bPlaying=FALSE;
  155.   }
  156. }
  157.  
  158. //
  159. // CLEARBUFFER
  160. //
  161. BOOL CDSUtil::ClearBuffer ()
  162. {
  163.   WAVEFORMATEX WaveFormat;
  164.   DWORD        dwSizeWritten;
  165.  
  166.   PBYTE        pPtr;
  167.   DWORD        dwSize;
  168.  
  169.   if (FAILED( lpDSB->GetFormat(&WaveFormat,sizeof(WAVEFORMATEX),&dwSizeWritten))) return FALSE;
  170.   if ( SUCCEEDED(lpDSB->Lock(0,0,(LPVOID *)&pPtr,&dwSize,NULL,NULL,DSBLOCK_ENTIREBUFFER)))
  171.   {
  172.      if (WaveFormat.wBitsPerSample==8) FillMemory(pPtr,dwSize,128);
  173.      else                              FillMemory(pPtr,dwSize,0);
  174.      lpDSB->Unlock(pPtr,dwSize,NULL,0);
  175.      return TRUE;
  176.   }
  177.   else return FALSE;
  178. }
  179.